package legoclient;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import ServerClient.ClientServerCmd;
import javax.swing.border.BevelBorder;

public class ClientGUI extends JFrame implements MouseListener, ActionListener {

    /**
     * @author Björn Lundgren, Eric Enger
     * @param args
     * 
     */
    // invariants
    private static int refresh = 100;
    private JLabel wabcamLabel;
    // status labels
    private JLabel serverStatus;
    private JLabel robotStatus;
    private JLabel webcamStatus;
    private JTextArea logTextArea = new JTextArea();
    // imageicons
    private ImageIcon pic = new ImageIcon(getClass().getResource("dud.png"));
    private ImageIcon noServerIcon = new ImageIcon(getClass().getResource("not_connected.gif"));
    private ImageIcon serverIcon = new ImageIcon(getClass().getResource("connected.gif"));
    private ImageIcon noRobotIcon = new ImageIcon(getClass().getResource("no_robot.gif"));
    private ImageIcon robotIcon = new ImageIcon(getClass().getResource("robot.gif"));
    private ImageIcon noWebcamIcon = new ImageIcon(getClass().getResource("no_webcam.gif"));
    private ImageIcon webcamIcon = new ImageIcon(getClass().getResource("webcam.gif"));
    public JTextField ipField = new JTextField("192.168.11.3");
    public JTextField tcpPortField = new JTextField("9070");
    public JTextField rtpPortField = new JTextField("9079");
    //private Timer timer = new Timer(refresh, new TickListener());
    private ClientBackend backend;
    private JmfPanel jmfPane;

    public ClientGUI() {
	super("Louie Control Panel");
            backend = new ClientBackend(this);
       
            JPanel serverPane = new JPanel();
            //serverPane.setLayout(new GridLayout(3,1));
            
            JPanel controlPane = new JPanel();
            controlPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED), "Manual Control"));
            controlPane.setLayout(new GridLayout(9,1));
            
            JButton connectButton = new JButton ("Connect");
            //JButton vConnectButton = new JButton ("Connect to Webcam");
            JButton shutDownButton = new JButton("Shutdown");
            JButton gotoButton = new JButton("Set Targets");
            JButton forwardButton = new JButton("Forward");
            JButton turnLeftButton = new JButton("Left");
            JButton turnRightButton = new JButton("Right");
            JButton stopButton = new JButton("Stop");
            JButton backwardButton = new JButton("Backward");
            
            //controlPane.add(vConnectButton);         
            controlPane.add(shutDownButton);
            //controlPane.add(gotoButton);
            controlPane.add(forwardButton);
            controlPane.add(turnLeftButton);
            controlPane.add(turnRightButton);
            controlPane.add(backwardButton);
            controlPane.add(stopButton);
                        
            JPanel settingsPane = new JPanel();
            settingsPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            settingsPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED), "Connection Settings"));
            
            JPanel ipFieldPane = new JPanel(new BorderLayout());
            ipFieldPane.add(new JLabel("Server IP:"), BorderLayout.WEST);
            ipFieldPane.add(ipField, BorderLayout.CENTER);
            settingsPane.add(ipFieldPane);
            
            JPanel tcpPortFieldPane = new JPanel(new BorderLayout());
            tcpPortFieldPane.add(new JLabel("TCP port:"), BorderLayout.WEST);
            tcpPortFieldPane.add(tcpPortField, BorderLayout.CENTER);
            settingsPane.add(tcpPortFieldPane);
            
            JPanel rtpPortFieldPane = new JPanel(new BorderLayout());
            rtpPortFieldPane.add(new JLabel("RTP port:"), BorderLayout.WEST);
            rtpPortFieldPane.add(rtpPortField, BorderLayout.CENTER);
            settingsPane.add(rtpPortFieldPane);
            settingsPane.add(connectButton);
            JPanel logPane = new JPanel();
            logPane.setLayout(new BorderLayout());
            logTextArea.setEditable(false);
            logTextArea.setBackground(Color.BLACK);
            logTextArea.setForeground(Color.WHITE);
            JScrollPane logScrollPane = new JScrollPane(logTextArea);
            logPane.add(logScrollPane, BorderLayout.CENTER);
            logPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED), "Log"));
            
            jmfPane = new JmfPanel(this, backend);
            //mainPane.add(jmf);

            //statusPane
            JPanel statusPane = new JPanel();
            
            //iconPane
            JPanel iconPane = new JPanel();
            serverStatus = new JLabel(noServerIcon);
            webcamStatus = new JLabel(noWebcamIcon);
            robotStatus = new JLabel(noRobotIcon);
            iconPane.add(serverStatus);
            iconPane.add(webcamStatus);
            iconPane.add(robotStatus);
            statusPane.setLayout(new BorderLayout());
            statusPane.add(iconPane, BorderLayout.WEST);


            //menubar
            JMenuBar menuBar = new JMenuBar();
            JMenu file = new JMenu("Tools");
            JMenuItem sensors = new JMenuItem("Sensors");
            file.add(sensors);
            menuBar.add(file);
            setJMenuBar(menuBar);

            setResizable(false);
            
            
            setLayout(new BorderLayout());
            
            JPanel webcamControlPane = new JPanel();
            webcamControlPane.setLayout(new BorderLayout());
            webcamControlPane.add(jmfPane, BorderLayout.CENTER);
            webcamControlPane.add(controlPane, BorderLayout.EAST);
            
            JPanel programControlPane = new JPanel();
            programControlPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            programControlPane.add(shutDownButton);
            
            JPanel mainPane = new JPanel();
            mainPane.setLayout(new BorderLayout());
            mainPane.add(webcamControlPane, BorderLayout.CENTER);
            mainPane.add(serverPane, BorderLayout.SOUTH);
            
            
            serverPane.setLayout(new BorderLayout());
            serverPane.add(settingsPane, BorderLayout.NORTH);
            serverPane.add(logPane, BorderLayout.CENTER);
            serverPane.add(programControlPane, BorderLayout.SOUTH);
            this.getContentPane().add(mainPane);
            
            //add(statusPane, BorderLayout.SOUTH);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            //setLocationRelativeTo(null);
            //timer.start();
            pack();
            setVisible(true);
            
            update("Client GUI started");
            //add listeners

            stopButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    System.out.println("stop");
                    backend.writeCmd(new ClientServerCmd(ClientServerCmd.STOP));
                }
            });

            forwardButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    System.out.println(ClientServerCmd.MOVE_FORWARD);
                    backend.writeCmd(new ClientServerCmd(ClientServerCmd.MOVE_FORWARD));
                }
            });

            turnLeftButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    System.out.println(ClientServerCmd.TURN_LEFT);
                    backend.writeCmd(new ClientServerCmd(ClientServerCmd.TURN_LEFT));
                }
            });

            turnRightButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    System.out.println(ClientServerCmd.TURN_RIGHT);
                    backend.writeCmd(new ClientServerCmd(ClientServerCmd.TURN_RIGHT));
                }
            });

            backwardButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    System.out.println(ClientServerCmd.MOVE_BACKWARD);
                    backend.writeCmd(new ClientServerCmd(ClientServerCmd.MOVE_BACKWARD));
                }
            });
            
            connectButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    backend.connectToServer(ipField.getText(), ClientBackend.tcpPort);
                    jmfPane.startStream("rtp://" + ipField.getText()+ ":" + rtpPortField.getText()+ "/video");
                }
            });
            
            /*vConnectButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    //backend.connectToServer(ipField.getText(), ClientBackend.tcpPort);
                    jmfPane.startStream("rtp://" + ipField.getText()+ ":" + rtpPortField.getText()+ "/video");
                    //backend.connectToWebcam(ipField.getText(), ClientBackend.rtpPort);
                }
            });*/
            
             shutDownButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                   // Clean up needed
                    System.exit(0);
                }
            });
    };
    
    public void update(String message) {
        if(!logTextArea.getText().isEmpty()) logTextArea.append("\n");
            logTextArea.append(message);
            logTextArea.setCaretPosition(logTextArea.getDocument().getLength());

        
    }
    
    public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    }

    public void mouseEntered(MouseEvent e) {
        
    }

    public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    //mousePressed was chosen over mouseClicked because if
    //mouseClicked is used, and the mouse is moved even a pixel
    //it is considered a drag, and will not fire.
    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

    }

    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

    }
    
}
